<?php

namespace App\Http\Controllers;

use App\Http\Requests\ClueTagIndexRequest;
use App\Http\Requests\ClueTagStoreRequest;
use App\Http\Requests\ClueTagUpdateRequest;
use App\Http\Resources\ClueTagResource;
use App\Models\ClueTag;
use Illuminate\Http\Request;

class ClueTagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param ClueTagIndexRequest $request
     * @return \Illuminate\Http\Resources\Json\ResourceCollection
     */
    public function index(ClueTagIndexRequest $request)
    {
        $type = $request->get('type');

        $query = ClueTag::query();

        if ($type !== 'all') $query->where('type', $type);

        $data = $query->get();
        return ClueTagResource::collection($data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  ClueTagStoreRequest $request
     * @return ClueTagResource
     */
    public function store(ClueTagStoreRequest $request)
    {
        $data = $request->all();

        $item = ClueTag::create($data);

        return new ClueTagResource($item);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  ClueTagUpdateRequest $request
     * @param  \App\Models\ClueTag      $clueTag
     * @return ClueTagResource
     */
    public function update(ClueTagUpdateRequest $request, ClueTag $clueTag)
    {
        $data = $request->all();
        $clueTag->update($data);
        return new ClueTagResource($clueTag);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @throws (a)
     * @param  \App\Models\ClueTag $clueTag
     * @return \Illuminate\Http\Response
     */
    public function destroy(ClueTag $clueTag)
    {
        $clueTag->delete();
        return $this->responseSuccess(null , 204);
    }

}
